import sys
import xlrd
import xlwt
import os
p=os.path.dirname(__file__)
parent_path = os.path.dirname(p)
sys.path.append(parent_path)
from ulits.excelclass import Excel
from xlutils.copy import copy
class InformationAction(object):#个人信息中心
    file1=parent_path+'/data/users.xls'
    file2=parent_path+'/data/userinfo.xls'
    def __init__(self,ID):
        self.path=InformationAction.file2
        self.sheetname='用户信息'
        self.ID=ID
        self.file1=InformationAction.file1
    def get_Information(self):#查看所有信息
        excel=Excel(self.path)
        msy=excel.returnlist(self.sheetname)
        msm=[]
        for i in msy:
            if self.ID==i[0]:
                msm.append(i)
        if len(msm)==0:
            print('未添加任何信息')
        else :
            for j,i in enumerate(msm,1):
                print('{}  ID:{}  收货人姓名:{}  收货人联系方式:{}  收货人地址:{}'.format(j,i[0],i[1],i[2],i[3]))
                print('')
        return msy,msm
    def add_Information(self):#增加信息
        while True:
            self.user_addname=input('收货人姓名(q/Q退出)：').strip()
            if self.user_addname.upper()=='Q':
                break
            else:
                self.user_addtel=input('收货人联系方式(起始为1的11位手机号):\n').strip()
                if self.user_addtel.isdigit() and len(self.user_addtel)==11 and self.user_addtel[0]=='1':
                    self.user_address=input('收货人地址：')
                    user_id=self.ID
                    list_add=[user_id,self.user_addname,self.user_addtel,self.user_address]
                    excel=Excel(self.path)
                    excel.add_data(list_add)
                    print('收货人:%s,收货人联系方式:%s,收货地址:%s。添加成功'%(self.user_addname,self.user_addtel,self.user_address))
                    break
                else :
                    print('输入格式错误重新输入')
    def update_Information(self):#修改信息
        choice_update=input('原信息将被移除，确认修改信息？y/Y确认,n/N退出').strip()
        if choice_update.upper()=='Y':
            InformationAction.del_Information(self)#删除要修改的地址
            InformationAction.add_Information(self)#添加要修改的地址
        else:
            pass
    def update_pwd(self):#修改密码
        new_pwd=input('请输入你的新密码：')
        msm=[]
        excel=Excel(self.file1)
        msy=excel.returnlist('用户账号密码')
        num=0
        for i in msy:
            msm.append(i[0])
        num=msm.index(self.ID)
        wb = xlrd.open_workbook(self.file1)
        new_wb = copy(wb)  # 将原有的Excel，拷贝一个新的副本
        new_sheet = new_wb.get_sheet(0) 
        new_sheet.write(num,1,new_pwd)
        new_wb.save(InformationAction.file1)
        print('修改成功')          
    def del_Information(self):#删除信息
        msm=[]
        infor_list=InformationAction.get_Information(self)
        msy=infor_list[0]
        for i in msy:
            if i[0]==self.ID:
                msm.append(i)
        chose_update=input('请输入你选择的编号：').strip()
        chose_update=int(chose_update)-1
        name=msm[chose_update]
        if name in msy:
            msy.remove(name)#删除list中的一个元素
            work_book = xlwt.Workbook()
            sheet = work_book.add_sheet(self.sheetname) # 添加表
            for m in range(0,len(msy)):
                for n in range(len(msy[m])):
                    sheet.write(m, n, msy[m][n])
            work_book.save(self.path) 
